package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("all")
@RestController("AdminSetmealController")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealMapper setmealMapper;


    // 新增套餐
    @PostMapping
    @CacheEvict(value = "setmealCache",key = "#setmealDTO.categoryId")
    public Result insert(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐:{}",setmealDTO);
        //新增保存套餐
        setmealService.saveSetmeal(setmealDTO);

        return Result.success();

    }

    // 分页查询
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询:{}",setmealPageQueryDTO);
        PageResult page= setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(page);
    }

    //根据id查询套餐信息，用于修改信息的回显功能
    @GetMapping("/{id}")
    public Result<SetmealDTO> getById(@PathVariable Long id){
        log.info("根据id查询套餐信息:{}",id);
        SetmealDTO setmealDTO= setmealService.getById(id);
        return Result.success(setmealDTO);
    }

    //修改套餐信息
    @PutMapping
    @CacheEvict(value = "setmealCache",key = "#setmealDTO.categoryId")
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息:{}",setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    //修改状态
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",key ="#result.data.categoryId")
    public Result<Setmeal> updateStatus(@PathVariable Integer status,Long id){
        log.info("修改状态:{}",status);
        //根据id查询到套餐所属的分类id
        Setmeal setmealS = setmealMapper.getById(id);
        setmealService.updateStatus(status,id);
        return Result.success(setmealS);
    }

    //批量删除或者单个删除
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除或者单个删除:{}",ids);
        setmealService.delete(ids);
        return Result.success();
    }

}
